package com.example.easyexceldemo.excel;

import org.easy.excel.aop.cell.CellInterceptorAdapter;
import org.easy.excel.aop.cell.CellJoinPoint;
import org.easy.excel.exception.ExcelException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.example.easyexceldemo.service.StudentService;

/**
 * 业务逻辑
 * 需要通过老师姓名查询数据库并校验数据同时为
 * teacherId 字段进行赋值
 * @author lisuo
 *
 */
@Component
public class TeacherCellInterceptor extends CellInterceptorAdapter {

	@Autowired
	private StudentService studentService;
	
	@Override
	public boolean supports(CellJoinPoint joinPoint) {
		return joinPoint.getValue()!=null;
	}

	@Override
	public Object executeImport(CellJoinPoint joinPoint) throws ExcelException {
		String teacher = (String) joinPoint.getValue();
		String teacherId =  studentService.findByTeacherName(teacher);
		if(teacherId==null) {
			//为空为了保证不丢失这个属性手动设置value
			joinPoint.getAccessor().setPropertyValue("teacher", teacher);
			throw newExcelDataException("["+teacher+"]查不到对应的教师信息", joinPoint);
		}else {
			joinPoint.getAccessor().setPropertyValue("teacherId", teacherId);
		}
		return super.executeImport(joinPoint);
	}

}
